//
//  PageView.m
//  PageTool
//
//  Created by Burn on 2020/3/6.
//  Copyright © 2020 menksoft. All rights reserved.
//

#define HvrStudioChaganTig_M(x)          [UIFont fontWithName:@"HvrStudioChaganTig-M" size:x] //标准编码使用

#import "PageView.h"
#import "PageCell.h"

@interface PageView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(strong,nonatomic) NSArray *ele;
@property(strong,nonatomic) UICollectionView *collectionView;

@end

static NSString *const ID = @"pageCellID";

@implementation PageView

- (instancetype)initWithFrame:(CGRect)frame element:(NSArray *)ele{
    if (self == [super initWithFrame:frame]) {
        self.ele = ele;
        [self setui];
    }
    return self;
}

- (void)setui{
    [self addSubview:self.collectionView];
    
    UIButton *topPage = [UIButton buttonWithType:UIButtonTypeCustom];
    topPage.transform = CGAffineTransformScale(topPage.transform, -1, 1);
    topPage.transform =  CGAffineTransformRotate(topPage.transform, M_PI / 2);
    topPage.frame = CGRectMake(0, 0, 40, self.frame.size.height);
    [topPage setTitle:@" \n" forState:UIControlStateNormal];
    topPage.titleLabel.font = HvrStudioChaganTig_M(17);
    [topPage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    topPage.titleLabel.numberOfLines = 0;
    [self addSubview:topPage];
    
    
    topPage.layer.borderWidth = .2;
    topPage.layer.borderColor = [UIColor lightGrayColor].CGColor;
    topPage.layer.cornerRadius = 3;
    topPage.layer.masksToBounds = YES;
    [topPage addTarget:self action:@selector(topPageClick) forControlEvents:UIControlEventTouchUpInside];

    
    UIButton *nextPage = [UIButton buttonWithType:UIButtonTypeCustom];
    nextPage.transform = CGAffineTransformScale(nextPage.transform, -1, 1);
    nextPage.transform =  CGAffineTransformRotate(nextPage.transform, M_PI / 2);
    nextPage.frame = CGRectMake(self.frame.size.width - 40, 0, 40, self.frame.size.height);
    [nextPage setTitle:@" \n" forState:UIControlStateNormal];
    [nextPage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    nextPage.titleLabel.numberOfLines = 0;
    nextPage.titleLabel.font = HvrStudioChaganTig_M(17);
    [self addSubview:nextPage];
    
    nextPage.layer.borderWidth = .2;
    nextPage.layer.borderColor = [UIColor lightGrayColor].CGColor;
    nextPage.layer.cornerRadius = 3;
    nextPage.layer.masksToBounds = YES;
    
    [nextPage addTarget:self action:@selector(nextPageClick) forControlEvents:UIControlEventTouchUpInside];

    
    if (self.ele.count == 1) {
        nextPage.hidden = YES;
        topPage.hidden = YES;
    }
    
    if (self.ele.count < 5) {
        topPage.frame = CGRectMake(self.collectionView.frame.origin.x - 50, 0, 40, self.frame.size.height);
        nextPage.frame = CGRectMake(CGRectGetMaxX(self.collectionView.frame), 0, 40, self.frame.size.height);
    }
}

- (void)topPageClick{
    if (self.page > 1) {
        self.page -= 1;
        [self scroll:NO];
    }
}

- (void)nextPageClick{
    if (self.page < self.ele.count) {
        self.page += 1;
        [self scroll:YES];
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.ele.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.index = indexPath.item;
    cell.pageSelected = indexPath.item == self.page - 1;
    
    return cell;
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == self.page - 1) return;
    BOOL next = self.page > indexPath.item ? NO : YES;
    self.page = indexPath.item + 1;
    [self scroll:next];
    
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumInteritemSpacing = 10;
//        layout.minimumLineSpacing = 20;
        layout.itemSize = CGSizeMake((self.frame.size.width - 100 - 40) / 5, 50);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        CGRect frame;
        CGFloat w;
        
        if (self.ele.count < 5) {
            w = 34 * self.ele.count + 10 * (self.ele.count);
            frame = CGRectMake((self.frame.size.width - w) * .5, 0, w, 50);
        }else{
            frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(0, 50, 0, 50));
        }
        
        
        _collectionView = [[UICollectionView alloc]initWithFrame:frame collectionViewLayout:layout];
        [_collectionView registerClass:[PageCell class] forCellWithReuseIdentifier:ID];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}


- (void)setPage:(NSInteger)page{
    _page = page;
    [self.collectionView reloadData];
    
}

- (void)scroll:(BOOL)next{
    
    UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:(next ? self.page + 1 : self.page - 3) inSection:0]];
    [self.collectionView scrollRectToVisible:attributes.frame animated:YES];
    
//    NSLog(@"%ld",self.page);
    if ([self.delegate respondsToSelector:@selector(currentIndex:)]) {
        [self.delegate currentIndex:self.page];
    }
}

@end




